Grokking Microservices Design Patterns
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
The Course at a Glance
Strangler Fig Pattern
Introduction
The Problem: Legacy Systems
The Strangler Pattern: A Solution
The Architecture of the Strangler Pattern
Strangler Pattern: A Detailed Example
Key Insights and Implications
API Gateway Pattern
Introduction to the API Gateway Pattern
Advantages of API Gateway Pattern
API Gateway Pattern: An Example
Performance Implications
System Design Example
Backends for Frontends (BFF) Pattern
Introduction to BFF
The Problem: Traditional Backend Models
The Architecture of the BFF Pattern
BFF Pattern: An Example
Performance Implications
System Design Examples
Service Discovery Pattern
What is Service Discovery Pattern?
The Problem: Service Coordination in Distributed Systems
Service Discovery Pattern: A Solution
The Architecture of the Service Discovery Pattern
The Inner Workings of the Service Discovery Pattern
Service Discovery Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Security Considerations
Circuit Breaker Pattern
Introduction
The Problem: The Struggles of Distributed Systems and Service Failures
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures
Circuit Breaker Pattern: An Example
Performance Implications and Special Considerations
System Design Examples
Summary
Introduction
The Problem: Failure Propagation in Distributed Systems
The Bulkhead Pattern: A Solution
The Architecture
The Inner Workings
Bulkhead Pattern: A Example
Performance Implications and Special Considerations
System Design Examples
Conclusion
Introduction
The Retry Pattern: A Solution to Unreliable External Resources
The Architecture of the Retry Pattern
Retry Pattern: An Example
Performance Implications
Use Cases and System Design Examples
Conclusion
Introduction to the Sidecar Pattern
The Problem: Monolithic Application Management
A Solution to the Monolithic Mayhem
The Architecture of the Sidecar Pattern
Sidecar Pattern: Bringing Theory to Practice with an Example
Performance Implications
System Design Examples: Bringing the Sidecar Pattern to Life
Introduction to Saga Pattern
The Problem: Traditional Transaction Models
The Saga Pattern: A Solution
The Architecture of the Saga Pattern
The Inner Workings of the Saga Pattern
Saga Pattern: A Example
Performance Implications
System Design Examples
Conclusion
Introduction
The Problem: Managing Complex Interactions in Distributed Systems
Event-Driven Architecture: A Promising Solution
The Architecture of the Event-Driven Architecture Pattern
The Inner Workings of the Event-Driven Architecture Pattern
Event-Driven Architecture Pattern: An Example
Performance Implications and Special Considerations
Use Cases and System Design Examples
Conclusion
Introduction
The Problem: Traditional CRUD Operations
CQRS Pattern: A Solution
The Architecture of the CQRS Pattern
The Inner Workings of the CQRS Pattern
CQRS Pattern: An Example
Issues, Special Considerations, and Performance Implications
System Design Examples
Introduction
The Problem: Configuration Management in a Microservices Architecture
The Solution: Configuration Externalization Pattern
Unveiling the Architecture: How Does Configuration Externalization Work?
Delving into Code: An Example
Considerations and Implications
Use Cases and Real-world Examples
Conclusion
Embrace the Future of Software Architecture
The Circuit Breaker Pattern: An Effective Shield Against Cascading Failures

The Circuit Breaker Pattern solves the above problems by failing fast and preventing repeated futile attempts once a service is known to be faulty. In practice, you wrap calls to an external service in a Circuit Breaker component that monitors the outcomes of those calls. Initially, the circuit breaker is in a Closed state, allowing calls to flow normally. It counts failures (such as exceptions or timeouts). When the number of failures reaches a certain threshold, the breaker "trips" to an Open state. In the Open state, calls are immediately rejected – for example, your code might instantly throw an exception or return a fallback value without even attempting the remote call. By doing this, the application avoids waiting on a likely unresponsive service, thus preserving its own responsiveness and resources.

Crucially, an open circuit also stops adding load on the failing service, which gives that service a chance to recover. After a predefined timeout period, the circuit breaker will enter a Half-Open state, where it allows a limited number of test calls to see if the external service is healthy again. If a test call succeeds, the breaker is reset back to Closed (meaning the service is back up, so resume normal operations). If the test call fails (e.g., the service is still down), the breaker returns to Open state immediately and the cycle repeats (the system will wait again before trying next time). In this way, the Circuit Breaker Pattern isolates failures and prevents them from cascading through the system by controlling the flow of calls based on service health. It prefers fast failure and graceful degradation over long waits and resource exhaustion, significantly enhancing overall system resilience.

Architecture & Inner Workings

Under the hood, a circuit breaker maintains some state and counters to decide whether to allow calls. There are typically three states in the Circuit Breaker finite-state machine:

  • Closed – Normal operations. The circuit is closed, so traffic flows to the service as usual. The breaker monitors the number of recent failures. If the number of failures exceeds a configured failure threshold (within a certain time window), the breaker transitions to Open state. While closed, successful calls generally reset the failure counter (so the occasional transient error won’t trip the breaker immediately).
  • Open – Circuit is “tripped.” The breaker has detected too many failures and opens the circuit. In this state, calls are blocked immediately – the application will typically get an error or fallback response without trying the real operation. Open state endures for a certain timeout period (the open interval). The idea is to prevent any calls to the troubled service for a little while, to avoid useless load and give it time to recover.
  • Half-Open – Trial mode. After the open timeout expires, the circuit breaker half-closes (goes to Half-Open). In this state, it will allow a limited number of test requests to pass through to the actual service. Think of this as cautiously probing the service: “Are you OK now?” If a test request succeeds, it’s a signal that the service is likely back up; the breaker resets to Closed state (often resetting failure counters). If a test request fails, the breaker immediately flips back to Open and the timeout period starts over. Only a few requests are let through in half-open to avoid blasting a recovering service with traffic. Often, one successful call is enough to close the circuit, but some implementations might require a few successes to be sure.
Circuit Breaker Pattern
Circuit Breaker Pattern

State Transitions: The transitions can be summarized as: Closed → Open when failures exceed the threshold; Open → Half-Open after the timeout elapses; Half-Open → Closed if the next call succeeds (or after a certain number of consecutive successes), or Half-Open → Open if the test call fails. The diagram below (if we had one) would show a loop: Closed → Open → (wait) → Half-Open → Closed (or back to Open). These states and transitions implement the logic of “stop calling when it’s likely down, and periodically check if it’s back up.”

Every call through the circuit breaker will perform a quick check of the state:

  • If Closed, just attempt the call and record the outcome.
  • If Open, skip the call and immediately return a failure (or use a fallback).
  • If Half-Open, allow the call (perhaps only a certain number of concurrent calls) and then adjust the state based on the result.

In essence, the circuit breaker acts as a safety valve in your architecture. It ensures that a failing component of the system doesn’t drag down the rest. By tuning the failure threshold and timeout, you can decide what “failure rate” warrants cutting off calls and how long to wait before giving the service another chance.

Integrating the Circuit Breaker Pattern

Integrating the Circuit Breaker pattern into your system does not necessarily require a complete system overhaul. In fact, it can be added as a protective layer around your existing service calls without disrupting the underlying service logic.

Essentially, every time a request is made to a service, it is made through the Circuit Breaker. It is the Circuit Breaker that determines whether to forward the request to the service based on its current state and the outcome of recent requests.

This way, the Circuit Breaker pattern can be introduced gradually into a system, starting with critical services that could have a significant impact on the system's functionality if they were to fail. Over time, as the benefits of the pattern become evident, it can be expanded to protect other services as well.

Circuit Breaker Settings: Finding the Right Balance

When implementing the Circuit Breaker pattern, it's crucial to focus on the configuration of its parameters. These include:

  • Failure Threshold: Determines when the circuit breaker switches from Closed to Open state. It's the limit of failed requests needed before activating the circuit breaker.
  • Timeout Period: The duration the Circuit Breaker remains in the Open state before moving to Half-Open. This is the recovery time given to the service.
  • Number of Requests in Half-Open State: This is how many requests are allowed through when the Circuit Breaker is in the Half-Open state, testing if the service has recovered.

The values for these parameters are vital. They must strike a balance between protecting the service, allowing it time to recover, and keeping the service as available as possible for handling requests. The ideal settings depend on several factors:

  • The nature of the service and the system.
  • The expected load on the service.
  • The criticality of the service to the system's overall functionality.

It's important to remember that these parameters shouldn't be static. They can and should be adjusted dynamically in response to the system's state and performance. For example, in times of high demand, adjustments might include:

  • Increasing the failure threshold, allowing the service to endure more failed requests before the Circuit Breaker activates.
  • Reducing the timeout period, enabling quicker recovery attempts, thus allowing the service to handle more requests during peak times.

These dynamic adjustments help in maintaining optimal service availability and performance, even under varying system conditions.

The Problem: The Struggles of Distributed Systems and Service Failures
Circuit Breaker Pattern: An Example
Mark as Completed

On this page

The Circuit Breaker Pattern Demystified

Three States of Operation: Closed, Open, and Half-Open

Integrating the Circuit Breaker Pattern

Circuit Breaker Settings: Finding the Right Balance